from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select("li[class='mb20 pb20 cleared']")

    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select("h2 a")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = "https://www.nature.com/" + li.select("h2 a")[0]["href"]
        except:
            pass
        # try:
        #     tempData["publishTime"] = li.select(".title_box ul.search-metaitem.comma_separated")[0].select('li')[0].text.strip()
        # except:
        #     pass

        allData.append(tempData)


    return allData

def getData(keyword,cookies):
    headers = {
        'authority': 'www.nature.com',
        'cache-control': 'max-age=0',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        # 'cookie': '__gads=ID=4435d3a104d0f1c6:T=1551422124:S=ALNI_MaMmfBllOCSaAegsOyFTkujj8eX1w; idp_session=sVERSION_159300340-74b8-4b46-ad18-1f581fca5783; idp_session_http=hVERSION_12fc9792f-9abf-4f32-8b7f-da2a3c13662b; idp_marker=8541a620-77b4-4fb3-bbfb-bb0164e00e16',
    }

    params = (
        ('q', keyword),
        ('order', 'relevance'),
    )

    response = requests.get('https://www.nature.com/search', headers=headers, params=params, cookies=cookies)

    allData = formData(response)

    return allData

def getCookies():
    headers = {
        'authority': 'www.nature.com',
        'cache-control': 'max-age=0',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'if-none-match': '"b16a335e893f74ca100f5ed228e8021b"',
    }
    s = requests.session()
    response = s.get('https://www.nature.com/', headers=headers)
    cookies = s.cookies.get_dict()
    return cookies


def index(request):


    # page = 1
    data = json.loads(request.POST.get('data', '1'))

    cookies = getCookies()

    allData = getData(data["values"][0],cookies)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")